<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/views/common/header.jsp" %>

<div class="box box-primary">
  <div class="box-header with-border">
  </div><!-- /.box-header -->
  <!-- form start -->
  <form action="/resource/save" name="form-file" id="form-file"  enctype="multipart/form-data" method="post" role="form">
    <div class="box-body">
      <input type="hidden" value="${res.id }" name="id" id="id">
      <div class="form-group">
        <label for="file">选择文件上传</label>
        <input type="file" id="file" name="file">
      </div>
      <div class="form-group">
        <label>一级栏目</label>
        <select class="form-control" name="fsttype" id="fsttype">
          <option value="0">选择一级栏目</option>
          <c:forEach var="type" items="${fst_list}">
          <option value="${type.id }" <c:if test="${type.id == res_type.id}"></c:if>>${type.name }</option>
          </c:forEach>
        </select>
      </div>
      <div class="form-group">
        <label>二级栏目</label>
        <select class="form-control" name="type" id="type">
          <option value="0">请先选择一级栏目</option>
        </select>
      </div>
      <div class="form-group">
        <label for="title">标题</label>
        <input type="text" class="form-control" id="title" name="title" placeholder="标题">
      </div>
      <div class="form-group">
        <label>描述</label>
        <textarea class="form-control" rows="3" placeholder="Enter ..." name="desc" id="desc"></textarea>
      </div>
      <div class="form-group">
        <label>下载权限</label>
        <select class="form-control" name="downtype">
          <option value="0">游客可下载</option>
          <option value="1">登录用户可下载</option>
        </select>
      </div>
    </div><!-- /.box-body -->

    <div class="box-footer">
      <button type="button" class="btn btn-primary" id="btn-file-upload">上传</button>
    </div>
  </form>
  <script>
  $(function(){
	  $('#fsttype').on('change', function (){
		  $.ajax({
			  'data': {'parent': $('#fsttype').val()},
			  'url': '/restype/getSubType',
			  'dataType': 'JSON',
			  'type': 'POST',
			  'success': function(ret){
				  if (parseInt(ret.code) == 200)
				  {
					  $('#type').empty()
					  var i = 0
					  for (i = 0; i < ret.data.length; i++)
					  {
						  var obj = ret.data[i]
						  var html = '<option value="'+obj.id+'">'+obj.name+'</option>'
						  $('#type').append(html)
					  }
				  }
			  }
		  })
	  })
	  
	  $('#btn-file-upload').on('click', function (){
		  if ($('input#file').val() == '')
		    {
		      console.log('empty file');
		      swal('没有选择文件', '', 'error');
		      return false;
		    }
		  var formData = new FormData($('#form-file')[0]);
		  $.ajax({
		         url: '/resource/save' ,
		         type: 'POST',
		         data: formData,
		         async: false,
		         cache: false,
		         contentType: false,
		         processData: false,
		         dataType: 'JSON',
		         success: function (ret) {
		             if (parseInt(ret.code) != 200)
		             {
		            	 swal('保存失败', ret.msg, 'error');
		            	 return false;
		             }
		             swal('成功', '', 'success');
		             setTimeout('window.location.href="/resource/list"', 3000);
		         },
		         error: function (ret) {
		         }
		    });
		    return false;
	  })
  })
  </script>
</div>

<%@ include file="/views/common/footer.jsp" %>